<?php
/**
 * 2021/3/18
 * Created by breeze
 * User: breeze
 */
namespace app\admin\business;
use app\common\model\Content;
use app\common\model\ContentCategory;
class contentBus
{
    public $contentCategory =null;
    public $content =null;
    public function __construct()
    {
        $this->contentCategory = new ContentCategory();
        $this->content = new Content();
    }

    public function addCategory($data){
        $data = $data;
        if (empty($data)){
            throw new \think\Exception('添加的类别名称不能为空');
        }
//        $contentCategory = new ContentCategory();
        $result = $this->contentCategory->insertDate($data);
        if(empty($result)){
            throw new \think\Exception('添加失败，检查数据');
        }
        return $result;
    }

    public function getCategoryList(){
        $result = $this->contentCategory->getList();
        if(empty($result)){
            throw new \think\Exception('未查询到数据');
        }
        return $result;
    }
    public function getDetail($id){
        if(empty($id)){
            throw new \think\Exception('获取失败，id不能为空');
        }

        $result = $this->contentCategory->getDetail($id);
        if(empty($result)){
            throw new \think\Exception('未查到该内容');
        }
        return $result;
    }

    /**
     * 删除category
     * @param $id
     * @return bool
     * @throws \think\Exception
     */
    public function removeCategory($id){
        $result = $this->contentCategory->remove($id);
        if(empty($result)){
            throw new \think\Exception("未查到删除的信息或者改信息已经删除");
        }
        return $result;
    }
    public function updateCategory($data){
        $result = $this->contentCategory->updateCategory($data);
        if(empty($result)){
            throw new \think\Exception("数据未更新，请查看数据");
        }
        return $result;
    }

    public function addContent($data){
        $result = $this->content->insertDate($data);
        if(empty($result)){
            throw new \think\Exception('添加失败，检查数据');
        }
        return $result;

    }
    public function getContentList(){
        $result = $this->content->getList();
        if(empty($result)){
            throw new \think\Exception('未查询到数据');
        }
        return $result;
    }
    public function getContentDetail($id){
        if(empty($id)){
            throw new \think\Exception('获取失败，id不能为空');
        }

        $result = $this->content->getDetail($id);
        if(empty($result)){
            throw new \think\Exception('未查到该内容');
        }
        return $result;
    }

    public function updateContent($data){
        $id = $data['id'];
        $updateDate = [
            "title" => $data['title'],
            "content" => $data['content']
        ];
        $result = $this->content->updateContent($id,$updateDate);
        if(empty($result)){
            throw new \think\Exception("数据未更新，请查看数据");
        }
        return $result;
    }
    public function removeContent($id){
        $result = $this->content->remove($id);
        if(empty($result)){
            throw new \think\Exception("未查到删除的信息或者改信息已经删除");
        }
        return $result;
    }
    public function changeContent($data){
        $id = $data['id'];
        $updateDate = [
            'sort'=>$data['sort']
        ];
        $result = $this->content->changeSort($id,$updateDate);
        if(empty($result)){
            throw new \think\Exception("数据未更新，请查看数据");
        }
        return $result;
    }
}